package com.jt.controller;

import com.jt.enums.SysLogEnum;
import com.jt.utils.HtmlToPdfUtils;
import com.jt.utils.log.SysLogAnnotation;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.aspectj.util.FileUtil;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;

@Slf4j
@RequestMapping("/html")
@RestController
public class HtmlToPdfController {
    @RequestMapping("/htmlToPdf")
    @SysLogAnnotation(operDesc = "html转pdf",operType = SysLogEnum.OTHER,operModul = "html转pdf")
    public void HtmlAsPdf(MultipartFile file, HttpServletResponse response) throws IOException {
        FileOutputStream out = null;
        InputStream in = null;
        File filePdfPathFile=null;
        String filename = file.getOriginalFilename();
        try {
            /*pdf路径*/
            String PdfPath = "D:/htmlToPdf/pdf";
            File filePdf = new File(PdfPath);
            if (!filePdf.isDirectory()) {
                filePdf.mkdirs();
            }
            String PdfFilePath = PdfPath + File.separator + UUID.randomUUID().toString() +".pdf";
            filePdfPathFile = new File(PdfFilePath);
            if (!filePdfPathFile.exists()) {
                filePdfPathFile.createNewFile();
            }
            /*html本地路径*/
            String filebenDiPath = "D:/htmlToPdf/localhostHtml";
            File fileLocalhost = new File(filebenDiPath);
            if (!fileLocalhost.isDirectory()) {
                fileLocalhost.mkdirs();
            }
            String fileBdPath = filebenDiPath + File.separator + filename;
            File fileBdPathFile = new File(fileBdPath);
            if (!fileBdPathFile.exists()) {
                fileBdPathFile.createNewFile();
            }
            in = file.getInputStream();
            out = new FileOutputStream(fileBdPathFile);
            int len = 0;
            byte[] bytes = new byte[1024];
            while ((len = in.read(bytes)) > 0) {
                out.write(bytes, 0, len);
            }
            FileInputStream fileInputStream=new FileInputStream(fileBdPathFile);
            FileOutputStream fileOutputStream=new FileOutputStream(filePdfPathFile);
            String SongTi="C:\\Users\\366064537\\Desktop\\hongheng\\springboot\\src\\main\\resources\\static\\yangshi\\simsun.ttc,1";
//            String hei="C:\\Users\\366064537\\Desktop\\hongheng\\springboot\\src\\main\\resources\\static\\yangshi\\simhei.ttf";
            HtmlToPdfUtils.convertToPdf(fileInputStream,"",SongTi,fileOutputStream);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            out.close();
            in.close();
        }
        response.setContentType("application/csv;charset=GBK");
        response.setHeader("Content-Disposition", "attachment;  filename=" + new String(filename.getBytes("GBK"), "ISO8859-1"));
        response.setCharacterEncoding("GBK");
        OutputStream outRep = response.getOutputStream();
        FileInputStream inResert = new FileInputStream(filePdfPathFile);
        int lenRes = 0;
        byte[] bytess = new byte[1024];
        while ((lenRes = inResert.read(bytess)) > 0) {
            outRep.write(bytess, 0, lenRes);
        }

    }


    /*将文件转换为string文本*/
    public static String fileAsString(File file) throws IOException {
        StringBuffer stringBuffer = new StringBuffer();
        FileReader fileReader = null;
        BufferedReader bufferedReader = null;
        try {
            fileReader = new FileReader(file);
            bufferedReader = new BufferedReader(fileReader);
            String lodeRow;
            while ((lodeRow = bufferedReader.readLine()) != null) {
                stringBuffer.append(lodeRow.trim());
            }
        } catch (Exception e) {
            log.error("读取文件[" + file.getName() + "]转换成文本异常：", e);
        } finally {
            try {
                if (fileReader != null) {
                    fileReader.close();
                }
                if (bufferedReader != null) {
                    bufferedReader.close();
                }

            } catch (Exception e) {
                e.getMessage();
            }
            finally {
                fileReader.close();
                bufferedReader.close();
            }

        }
        return stringBuffer.toString();
    }

}
